/**
 * 供外部post请求调用  
 */

let appid = wx.getAccountInfoSync().miniProgram.appId;

function post(url, params, onSuccess, onFailed) {
  request(url, params, "POST", onSuccess, onFailed);
}

/**
 * 供外部get请求调用
 */
function get(url, params, onSuccess, onFailed) {
  request(url, params, "GET", onSuccess, onFailed);
}

function request(url, params, method, onSuccess, onFailed) {
  wx.showLoading({
    title: "正在加载中...",
  })
  if(url != '/mini/v1/user/login') {
    if (!wx.getStorageSync("token")) {
      wx.redirectTo({
        url: '/pages/login'
      })
      wx.hideLoading();
      return;
    }
  }
  wx.request({
    // url: 'https://mocar.back.xzozo.com/api' + url,
    url: 'https://api.mocar.xzozo.com/api' + url,
    data: dealParams(params),
    method: method,
    header: { 
      'content-type': 'application/json',
      'Authorization': wx.getStorageSync("token"),
      'os': 'android',
      'version': '1.0.0',
      'device_token': 'ebc9f523e570ef14'
    },
    success: function (res) {
      wx.hideLoading();
      if (res.data) {
        /** start 根据需求 接口的返回状态码进行处理 */
        if (res.statusCode == 200) {
          if (res.data.code == 0 || res.data.code == 1) {
            onSuccess(res.data);
            return;
          } else if (res.data.code == 1006) {
            wx.removeStorage({
              key: 'token',
              key: 'Identity'
            });
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
            wx.redirectTo({
              url: '/pages/login',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        } else {
          wx.hideLoading();
        }
        /** end 处理结束*/
      }
    },
    fail: (res) => {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请求超时，请重试！'
      })
    }
  })
}

/**
 * function: 根据需求处理请求参数：添加固定参数配置等
 * @params 请求参数
 */
function dealParams(params) {
  params.shop_app_id = appid;
  return params;
}

function transTime(unixtime) {
  var dateTime = new Date(parseInt(unixtime) * 1000)
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var hour = dateTime.getHours();
  var minute = dateTime.getMinutes();
  var second = dateTime.getSeconds();
  var now = new Date();
  var now_new = Date.parse(now.toDateString());
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  return timeSpanStr;
}
function transTime1(unixtime) {
  var dateTime = new Date(parseInt(unixtime) * 1000)
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var now = new Date();
  var now_new = Date.parse(now.toDateString());
  var milliseconds = now_new - dateTime;
  var timeSpanStr = year + '-' + (month>10 ? month : '0' + month) + '-' + day;
  return timeSpanStr;
}

// 1.通过module.exports方式提供给外部调用
module.exports = {
  postRequest: post,
  getRequest: get,
  transTime: transTime,
  transTime1
}